import { ref, onMounted } from 'vue';
import { getAllTypeApi } from '@/api/sj/productManagement/finalDraft/index';
import { getDesignerAcceptOrderApi } from '@/api/sj/productManagement/designerTakesOrders/index';
import { getWorkingProcedureListApi } from '@/api/sj/productManagement/finalDraft/index';

const useFinalDraft = () => {
    const { proxy } = getCurrentInstance() as ComponentInternalInstance;

    const finalDraftVisible = ref(false);
    const finalDraftObj = ref<any>();
    const allDesignerList = ref<Array<{ id: number; name: string }>>([]);
    const finalDraftDetails = ref<any>();

    /** 获取所有定制类型 */
    const getAllDesignerListApi = async () => {
        const { code, data } = await getAllTypeApi();
        if (code == 200) {
            allDesignerList.value = data
        }
    };

    /** 打开定稿弹窗 */
    const handleFinalDraft = (row: any) => {
        if (row.acceptId) {
            finalDraftObj.value = row
            getDetails(row.id)
            // finalDraftVisible.value = true;
        } else {
            proxy?.$modal.msgWarning('该订单未被接单,请先接单');
        }
    };

    /** 获取详情接口 */
    const getDetails = async (id: string) => {
        try {
            const { code, data } = await getDesignerAcceptOrderApi(id)

            if (code == 200) {
                finalDraftDetails.value = await handleData(data)
                // historyUrl.value = data.historyUrl
                finalDraftVisible.value = true;
            }
        } catch (error) {


        }
    };

    /** 数据处理 */
    const handleData = async (data: any) => {
        const workVoNoList = await getIdsWorkingProcedure(allDesignerList.value[0].id)

        await Promise.all(data.relationVos.map(async (relationItem: any) => {
            relationItem.needShow = relationItem.needDesign == 1 ? true : false
            relationItem.workProcedureIds = []
            if (!relationItem.customizedTypeId || relationItem.needDesign != 1) {
                relationItem.customizedTypeId = allDesignerList.value[0].id
                relationItem.workVoNoList = workVoNoList

                if (workVoNoList.length != 0) {
                    workVoNoList.forEach((workItem: any) => {
                        if (workItem.defaultFlag == 1) {
                            relationItem.workProcedureIds.push(workItem.workProcedureId)
                        }
                    })
                }

            } else {
                relationItem.workVoNoList = await getIdsWorkingProcedure(relationItem.customizedTypeId)

                if ('workVoList' in relationItem) {
                    relationItem.workVoList.forEach((workItem: any) => {
                        if (workItem.chooseFlag == 1) {
                            relationItem.workProcedureIds.push(workItem.id)
                        }
                    })
                }
            }
        }));

        return data
    };

    /** 根据 id 获取 工序 */
    const getIdsWorkingProcedure = (ids: number) => {
        return new Promise<any>(async (resolve, reject) => {
            const { code, data } = await getWorkingProcedureListApi({ id: ids })

            if (code == 200) {
                resolve(data)
            } else {
                reject(false)
            }
        })
    };



    /** 确认提交 */
    const handleFinalDraftSubmit = (result: boolean, fun: Function) => {
        if (result) {
            fun();
        };
    };

    /** 关闭定稿弹窗 */
    const handleFinalDraftClose = () => {
        finalDraftVisible.value = false;
    };


    onMounted(() => {
        getAllDesignerListApi();
    });


    return {
        finalDraftVisible,
        finalDraftObj,
        allDesignerList,
        finalDraftDetails,
        handleFinalDraft,
        handleFinalDraftSubmit,
        handleFinalDraftClose
    }
};

export default useFinalDraft;
